package cn.kgc.shiro03.exception;

import cn.kgc.shiro03.util.ResponseResult;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author: mosin
 * @version: v1.0  2022/11/22
 * 全局异常处理
 */
@RestControllerAdvice
public class GlobalException {

    @ExceptionHandler(Exception.class)
    public ResponseResult  exception(Exception e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.setMsg("操作失败");
        responseResult.setCode(500);
        return responseResult;
    }


    @ExceptionHandler(UnknownAccountException.class)
    public ResponseResult  unknownAccountException(UnknownAccountException e){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setMsg("用户名错误");
        responseResult.setCode(500);
        return  responseResult;
    }


    @ExceptionHandler(IncorrectCredentialsException.class)
    public ResponseResult  incorrectCredentialsException(IncorrectCredentialsException e){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setMsg("密码错误");
        responseResult.setCode(500);
        return  responseResult;
    }

    @ExceptionHandler(UnauthorizedException.class)
    public ResponseResult  unauthorizedException(UnauthorizedException e){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setMsg("没有权限，请联系管理员");
        responseResult.setCode(500);
        return  responseResult;
    }

    @ExceptionHandler(LockedAccountException.class)
    public ResponseResult  lockedAccountException(LockedAccountException e){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setMsg("用户被锁定,请联系管理员");
        responseResult.setCode(500);
        return  responseResult;
    }




}
